import tool from "@/utils/tool";

export const checkButtonShow = (button, data, fieldList) => {
  if ((button.conditionList || []).length === 0) return true;

  let result = false;
  (tool.groupBy(button.conditionList, (item) => [item.groupIndex || 999]) || []).forEach(conditionList => {
    result = result || checkConditionList(conditionList, data, fieldList);
  });
  return result;
};

export const checkConditionList = (conditionList, data, fieldList) => {
  let result = true;
  (conditionList || []).forEach(condition => {
    const field = fieldList.find(item => item.id === condition.fieldId);
    if (!field) return false;
    const testValue = data[field.fieldName];
    switch(condition.type) {
      case "包含":
        result = result && (testValue || '').indexOf(condition.value || '') >= 0;
        break;
      case "不包含":
        result = result && (testValue || '').indexOf(condition.value || '') === -1;
        break;
      case "等于":
        result = result && (testValue || '') === (condition.value || '');
        break;
      case "不等于":
        result = result && (testValue || '') !== (condition.value || '');
        break;
      case "大于":
        result = result && parseFloat(testValue || '0') > parseFloat(condition.value || '0');
        break;
      case "大于等于":
        result = result && parseFloat(testValue || '0') >= parseFloat(condition.value || '0');
        break;
      case "小于":
        result = result && parseFloat(testValue || '0') < parseFloat(condition.value || '0');
        break;
      case "小于等于":
        result = result && parseFloat(testValue || '0') <= parseFloat(condition.value || '0');
        break;
    }
  });
  return result;
};